package config;

import com.baomidou.mybatisplus.generator.config.OutputFile;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description config
 * @Author zhengzetao
 * @Date 2022/9/16 16:31
 */
@Data
public class PackageConfig extends Config{

    final private static String GROUP = "PackageConfig";

    //parent(String)	父包名	默认值:com.baomidou
    private String parent;
    //moduleName(String)	父包模块名	默认值:无
    private String moduleName;
    //entity(String)	Entity 包名	默认值:entity
    private String entity;
    //service(String)	Service 包名	默认值:service
    private String service;
    //serviceImpl(String)	Service Impl 包名	默认值:service.impl
    private String serviceImpl;
    //mapper(String)	Mapper 包名	默认值:mapper
    private String mapper;
    //xml(String)	Mapper XML 包名	默认值:mapper.xml
    private String xml;
    //controller(String)	Controller 包名	默认值:controller
    private String controller;
    //other(String)	自定义文件包名	输出自定义文件时所用到的包名
    private String other;
    //pathInfo(Map<OutputFile, String>)	路径配置信息	Collections.singletonMap(OutputFile.mapperXml, "D://")
    //pathInfo.entity,pathInfo.service,pathInfo.serviceImpl,pathInfo.mapper,pathInfo.xml,pathInfo.controller,pathInfo.other
    private Map<OutputFile,String> pathInfo;

    private PackageConfig() {
        super(GROUP);
        this.parent = getStr("parent","com.baomidou");
        this.moduleName = getStr("moduleName");
        this.entity = getStr("entity","entity");
        this.service = getStr("service","service");
        this.serviceImpl = getStr("serviceImpl","service.impl");
        this.mapper = getStr("mapper","mapper");
        this.xml = getStr("xml","mapper.xml");
        this.controller = getStr("controller","controller");
        this.other = getStr("other");
        this.pathInfo = new HashMap<>();
        configPathInfo(OutputFile.entity,"pathInfo.entity");
        configPathInfo(OutputFile.service,"pathInfo.service");
        configPathInfo(OutputFile.serviceImpl,"pathInfo.serviceImpl");
        configPathInfo(OutputFile.mapper,"pathInfo.mapper");
        configPathInfo(OutputFile.xml,"pathInfo.xml");
        configPathInfo(OutputFile.controller,"pathInfo.controller");
        configPathInfo(OutputFile.other,"pathInfo.other");
    }

    private void configPathInfo(OutputFile outputFile, String key){
        if(containsKey(key)){
            this.pathInfo.put(outputFile,getStr(key,null));
        }
    }

    public static void setting(com.baomidou.mybatisplus.generator.config.PackageConfig.Builder builder){
        PackageConfig packageConfig = new PackageConfig();
        builder.parent(packageConfig.parent).moduleName(packageConfig.moduleName).entity(packageConfig.entity)
                .service(packageConfig.service).serviceImpl(packageConfig.serviceImpl).mapper(packageConfig.mapper)
                .xml(packageConfig.xml).controller(packageConfig.controller).other(packageConfig.other)
                .pathInfo(packageConfig.pathInfo);
    }
}
